import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/chat/ChatView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/LoginView.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register/RegisterView.vue')
  },
  {
    path: '/chat',
    name: 'Chat',
    component: () => import('../views/chat/ChatView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/discuss',
    name: 'Discuss',
    component: () => import('../views/discuss/DiscussListView.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/discuss/:id',
    name: 'DiscussDetail',
    component: () => import('../views/discuss/DiscussDetailView.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/discuss/add',
    name: 'DiscussAdd',
    component: () => import('../views/discuss/DiscussAddView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/discuss/my',
    name: 'DiscussMy',
    component: () => import('../views/discuss/DiscussMyView.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/file/analyze',
    name: 'FileAnalyze',
    component: () => import('../views/upload/FileAnalyzeView.vue'),
    meta: { requiresAuth: true }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  const isAuthenticated = localStorage.getItem('jwt_token');
  
  if (requiresAuth && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

export default router; 